<?php

require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();

 
function getConnection() {
    $dbhost = "127.0.0.1";
    $dbuser = "root";
    $dbpass = "";
    $dbname = "backbone";
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $dbh;
}

function findAll22($table,$page) {
    $page = ($page - 1) * 10;
    $sql_list = "SELECT * FROM $table LIMIT " . $page . ", 10";
    $sql_count = "SELECT count(*) FROM $table ";
    try {
        
        $stmt = getConnection()->prepare($sql_list);
        $stmt->execute();
        $rst_find = $stmt->fetchAll(PDO::FETCH_OBJ);
        $result = getConnection()->prepare($sql_count);
        $result->execute();
        $rst_count = $result->fetchColumn();
        $db = null;
        echo '{"result":' . json_encode($rst_find) . ', "count" :' . $rst_count . ' }';
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
        return;
    }
}

function selectAll($table = array(), $condicion = array(), $relacion = array()) {
    $result = array();
    try {
        if (!array($table)) {
            die("Las Tablas deben de estar en formato arreglo");
        }
        if (count($table) > 1) {
            foreach ($table as $id => $valor) {
                if ($id == 0) {
                    $tablas = $valor . " INNER JOIN " . next($table) . " ON(" . $relacion[$id] . ")";
                } else {
                    if ($id != 1) {
                        $tablas.= " INNER JOIN " . $valor . " ON(" . $relacion[$id - 1] . ")";
                    }
                }
            }
        } else {
            $tablas = $table[0];
        }
        if (empty($condicion)) {
            $sql = "SELECT * FROM $tablas";
            $this->_query = getConnection()->prepare($sql);
            $this->_query->execute();
        } else {
            foreach ($condicion as $key => $value) {
                $parametros[] = "$key=?";
                $condic[] = $value;
            }
            $condiciones = implode(" AND ", $parametros);
            $sql = "SELECT * FROM $tablas WHERE $condiciones";
            $this->_query = getConnection()->prepare($sql);
            $this->_query->execute($condic);
        }
        while ($this->_row = $this->_query->fetch()) {
            $result[] = $this->_row;
        }
        return $result;
    } catch (PDOExeption $e) {
        echo $e->getMessaage();
    }
}

function authorize($role = "user") {
    return function () use ( $role ) {
                //echo "authorize1";
                $app = new \Slim\Slim();
                if (!empty($_SESSION['user'])) {
                    //echo "authorize2";
                    if ($_SESSION['user']['role'] == $role) {
                        //echo "authorize3";
                        return true;
                    } else {
                        // If a user is logged in, but doesn't have permissions, return 403
                        $app->halt(403, 'You shall not pass!');
                    }
                } else {
                    // If a user is not logged in at all, return a 401
                    $app->halt(401, 'You shall not pass!');
                }
            };
}

?>
